package cn.tedu.sp06ribbon.controller;

import cn.tedu.sp01.pojo.Item;
import cn.tedu.sp01.pojo.Order;
import cn.tedu.sp01.pojo.User;
import cn.tedu.sp01.vo.JsonResult;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class RibbonController {
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/item-service/{orderId}")
    @HystrixCommand(fallbackMethod = "getItemsFB")
    public JsonResult<List<Item>> getItems(@PathVariable String orderId){
        return restTemplate.getForObject("http://item-service/{1}",JsonResult.class,orderId);
    }


    @PostMapping("/item-service/decreaseNumber")
    @HystrixCommand(fallbackMethod = "decreaseNumberFB")
    //post提交,无法使用浏览器提交,只能通过postman来进行测试
    public JsonResult decreaseNumber(@RequestBody List<Item> items){
        //@RequestBody 一般与post提交一起用,当post提交参数是一个对象时,就需要用到这个注解
        return restTemplate.postForObject("http://item-service/decreaseNumber",items,JsonResult.class);

    }
    @HystrixCommand(fallbackMethod = "getUserFB")
    @GetMapping("/user-service/{userId}")
    public JsonResult<User> getUser(@PathVariable Integer userId){
        return restTemplate.getForObject("http://user-service/{1}",JsonResult.class,userId);
    }
    @HystrixCommand(fallbackMethod = "addScoreFB")
    @GetMapping("/user-service/{userId}/score")
    public JsonResult addScore(@PathVariable Integer userId,Integer score){
        return restTemplate.getForObject("http://user-service/{1}/score?score={2}",JsonResult.class,userId,score);
    }
    @HystrixCommand(fallbackMethod = "getOrderFB")
    @GetMapping("/order-service/{orderId}")
    public JsonResult<Order> getOrder(@PathVariable String orderId){
        return restTemplate.getForObject("http://order-service/{1}",JsonResult.class,orderId);
    }
    @HystrixCommand(fallbackMethod = "addOrderFB")
    @GetMapping("/order-service")
    public JsonResult addOrder(){
        return restTemplate.getForObject("http://order-service",JsonResult.class);
    }

    // 降级方法

    //降级方法的参数和返回值，需要和原始方法一致，方法名任意
    public JsonResult<List<Item>> getItemsFB(String orderId) {
        return JsonResult.err("获取订单商品列表失败");
    }
    public JsonResult decreaseNumberFB(List<Item> items) {
        return JsonResult.err("更新商品库存失败");
    }
    public JsonResult<User> getUserFB(Integer userId) {
        return JsonResult.err("获取用户信息失败");
    }
    public JsonResult addScoreFB(Integer userId, Integer score) {
        return JsonResult.err("增加用户积分失败");
    }
    public JsonResult<Order> getOrderFB(String orderId) {
        return JsonResult.err("获取订单失败");
    }
    public JsonResult addOrderFB() {
        return JsonResult.err("添加订单失败");
    }

  /*  @GetMapping("/item-service/{orderId}")
    public JsonResult<List<Item>> getItems(@PathVariable String orderId){
        return restTemplate.getForObject("http://localhost:8001/{1}",JsonResult.class,orderId);
    }

    @PostMapping("/item-service/decreaseNumber")
    //post提交,无法使用浏览器提交,只能通过postman来进行测试
    public JsonResult decreaseNumber(@RequestBody List<Item> items){
        //@RequestBody 一般与post提交一起用,当post提交参数是一个对象时,就需要用到这个注解
        return restTemplate.postForObject("http://localhost:8001/decreaseNumber",items,JsonResult.class);

    }

    @GetMapping("/user-service/{userId}")
    public JsonResult<User> getUser(@PathVariable Integer userId){
        return restTemplate.getForObject("http://localhost:8101/{1}",JsonResult.class,userId);
    }

    @GetMapping("/user-service/{userId}/score")
    public JsonResult addScore(@PathVariable Integer userId,Integer score){
        return restTemplate.getForObject("http://localhost:8101/{1}/score?score={2}",JsonResult.class,userId,score);
    }

    @GetMapping("/order-service/{orderId}")
    public JsonResult<Order> getOrder(@PathVariable String orderId){
        return restTemplate.getForObject("http://localhost:8201/{1}",JsonResult.class,orderId);
    }
    @GetMapping("/order-service")
    public JsonResult addOrder(){
        return restTemplate.getForObject("http://localhost:8201",JsonResult.class);
    }
*/


}
